package com.czx.controller;


import com.czx.entity.MyPage;
import com.czx.entity.User;
import com.czx.common.security.UserTokenThreadLocal;
import com.czx.service.FollowService;
import com.czx.service.SysMessageService;
import com.czx.service.UserService;
import com.czx.common.utils.R;
import com.czx.vo.FollowerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller

public class FollowerController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private FollowService followService;
    @Autowired
    private UserService userService;
    @Autowired
    private SysMessageService sysMessageService;

    @RequestMapping("/follow")
    @ResponseBody
    public R follow(@RequestParam("userId") Integer followUserId, Model model) {
        FollowerVo followerVo = new FollowerVo();
        followerVo.setFollowerId(followUserId);
        followerVo.setUserId(UserTokenThreadLocal.get().getUser().getId());
        // 调用关注服务进行关注操作
        int success = followService.follower(followerVo);

        if (success == 0) {
            sysMessageService.addFollow(followerVo);
            return R.success("关注成功");
        } else {
            return R.fail("取消关注");
        }
    }
    //关注的人
    @RequestMapping("/findFollowerListByUserId")
    public String findFollowerListByUserId(@RequestParam("userId") Integer id, Model model,MyPage page) {
        page= userService.getFollowById(id,page);
        model.addAttribute("page",page);
        User targetUser = userService.getUserById(id);
        model.addAttribute("targetUser",targetUser);
        return "site/followee";
    }
    @RequestMapping("/findFansListByUserId")
    public  String findFansListByUserId(@RequestParam("userId") Integer id, Model model,MyPage page){
       page=userService.getFansById(id,page);
       model.addAttribute("page",page);
        User targetUser = userService.getUserById(id);
        model.addAttribute("targetUser",targetUser);
       return "site/fans";
    }

}



